//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
    ///<summary>
    /// Base class for standard conditional formatting rules
    ///</summary>
    public abstract class StandardConditionalFormattingRule: ConditionalFormattingRule
    {
        protected StandardConditionalFormattingRule()
        {
            DifferentialStyleRecord = new DifferentialStyleRecord();
        }
        internal DifferentialStyleRecord DifferentialStyleRecord { get; set; }
        internal override sealed void ReadInner(ReadContext context)
        {
            int? dxfId = context.GetOptionalInteger("dxfId");
            if (dxfId != null)
            {
                var workbook = context.GetData<Workbook>();
                var r = context.GetData<DifferentialStyleRecordResolver>();
                DifferentialStyleRecord = r[dxfId.Value];
            }
            ReadInner2(context);
        }

        protected virtual void ReadInner2(ReadContext context){}

        internal override sealed void WriteInner(WriteContext c)
        {
            var t = c.GetData<WriteObjectTable<DifferentialStyleRecord>>();
            int DxfId = t[this.DifferentialStyleRecord];
            c.SetOptionalInteger("dxfId", DxfId);
            WriteInner2(c);
                
        }

        internal virtual void WriteInner2(WriteContext context)
        {
            
        }

        internal override ConditionalFormattingRule Clone()
        {
            var result = (StandardConditionalFormattingRule) MemberwiseClone();
            result.DifferentialStyleRecord = DifferentialStyleRecord.Clone();
            return result;
        }

        protected bool ApplyStyle(Cell cell, bool applies)
        {
            if (applies)
            {
                cell.DifferentialStyleRecord = DifferentialStyleRecord;
                return true;
            }
            if (cell.DifferentialStyleRecord == DifferentialStyleRecord)
                cell.DifferentialStyleRecord = null;
            return false;
        }

        public Fill Background
        {
            get { return DifferentialStyleRecord.Background; }
            set { DifferentialStyleRecord.Background = value; NotifyPropertyChanged("Background"); }
        }

        public Font Font
        {
            get { return DifferentialStyleRecord.Font; }
            set { DifferentialStyleRecord.Font = value;NotifyPropertyChanged("Font"); }
        }

        protected void ApplyStyle(Cell cell)
        {
            cell.DifferentialStyleRecord = DifferentialStyleRecord;
        }

        internal void ForgetStyle(Cell cell)
        {
            cell.DifferentialStyleRecord = null;
        }
    }
}
